package cn.tx.erp.sys.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.tx.erp.sys.domain.Log;
import cn.tx.erp.sys.service.LogService;
import cn.tx.erp.sys.utils.ActiverUser;
import cn.tx.erp.sys.utils.ResultObj;
import cn.tx.erp.sys.utils.WebUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

/**
 * 登录控制器
 * @author Mxb
 * @version 1.0
 * @date 2020/1/22 21:39
 */
@RestController
@RequestMapping("login")
public class LoginController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private LogService logService;

    /**
     * 用户登录方法
     * @param loginname
     * @param userpwd
     * @return
     */
    @RequestMapping("login")
    public ResultObj login(String loginname, String userpwd, String code, HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        log.info("接收到登录的验证码：" + code);
        log.info("接收到登录的用户名：" + loginname);
        log.info("接收到登录的密码：" + userpwd);
        String sessionCode = (String) session.getAttribute("code");
        if (!code.equals(sessionCode)) {
            //验证码不对
            return ResultObj.LOGIN_ERROR_CODE;
        }
        AuthenticationToken token = new UsernamePasswordToken(loginname, userpwd);
        try {
            subject.login(token);
            ActiverUser activerUser = (ActiverUser) subject.getPrincipal();
            // 设置session
            WebUtils.getSession().setAttribute("user", activerUser.getUser());

            // 记录登录日志
            Log log = new Log();
            log.setLoginname(activerUser.getUser().getName() + "_" + activerUser.getUser().getLoginname());
            log.setLoginip(WebUtils.getRequest().getRemoteAddr());
            log.setLogintime(new Date());
            logService.save(log);

            return ResultObj.LOGIN_SUCCESS;
        } catch (AuthenticationException e) {
            e.getMessage();
            return ResultObj.LOGIN_ERROR;
        }
    }

    /**
     * 生成验证码
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("code")
    public void createImageCode(HttpServletResponse response, HttpSession session) throws IOException {
        log.info("--------生成验证码执行---------");
        LineCaptcha lc = CaptchaUtil.createLineCaptcha(200, 60, 6, 4);
        //输出code
        log.info("验证码：" + lc.getCode());
        // 放到session域中
        session.setAttribute("code", lc.getCode());
        lc.write(response.getOutputStream());
        response.getOutputStream().close();
        // 第二种方式
        //ServletOutputStream outputStream = response.getOutputStream();
        //ImageIO.write(lc.getImage(), "JPEG", outputStream);
    }
}
